import Vue from 'vue'
import VueRouter from 'vue-router'

const login = () =>
  import(/* webpackChunkName: "login_home-welcome" */ '../components/login.vue')

const home = () =>
  import(/* webpackChunkName: "login_home-welcome" */ '../components/home.vue')

const welcome = () =>
  import(
    /* webpackChunkName: "login_home-welcome" */ '../components/welcome.vue'
  )
const users = () =>
  import(/* webpackChunkName: "users" */ '../components/user.vue')

const rights = () =>
  import(/* webpackChunkName: "rights" */ '../components/power/rights.vue')
const roles = () =>
  import(/* webpackChunkName: "roles" */ '../components/power/roles.vue')
const categories = () =>
  import(
    /* webpackChunkName: "categories" */ '../components/good_cate/categories'
  )
const params = () =>
  import(/* webpackChunkName: "params" */ '../components/good_cate/params')
const list = () =>
  import(/* webpackChunkName: "list" */ '../components/good_cate/list')
const add = () =>
  import(/* webpackChunkName: "add" */ '../components/good_cate/Add')
const order = () =>
  import(/* webpackChunkName: "order" */ '../components/order')
const reports = () =>
  import(/* webpackChunkName: "reports" */ '../components/reports')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: login },
  {
    path: '/home',
    component: home,
    children: [
      { path: '/home', redirect: '/welcome' },
      { path: '/welcome', component: welcome },
      { path: '/users', component: users },
      { path: '/rights', component: rights },
      { path: '/roles', component: roles },
      { path: '/categories', component: categories },
      { path: '/params', component: params },
      { path: '/goods', component: list },
      { path: '/goods/add', component: add },
      { path: '/orders', component: order },
      { path: '/reports', component: reports }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenstr = window.sessionStorage.getItem('token')
  if (!tokenstr) return next('/login')
  next()
})

export default router
